package com.tensquare.qa.controller;

import com.tensquare.qa.client.BaseClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private BaseClient baseClient;

    /**
     * 通过qa调用base的findById方法
     */
    @RequestMapping(value = "/label/{labelId}",method = RequestMethod.GET)
    public Result findByLabelId(@PathVariable("labelId") String labelId){
        Result result = baseClient.findById(labelId);
        return result;
    }

    @RequestMapping(value = "/newlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
    public Result newlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
        Page<Problem> pageData = problemService.newlist(labelid,page,size);
        return new Result(true, StatusCode.OK,"查询成功",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
    }

    @RequestMapping(value = "/hotlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
    public Result hotlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
        Page<Problem> pageData = problemService.hotlist(labelid,page,size);
        return new Result(true, StatusCode.OK,"查询成功",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
    }

    @RequestMapping(value = "/waitlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
    public Result waitlist(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
        Page<Problem> pageData = problemService.waitlist(labelid,page,size);
        return new Result(true, StatusCode.OK,"查询成功",new PageResult<Problem>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 增加一个问题
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Problem problem){
        //进行用户鉴权
        String token = (String) request.getAttribute("claims_user");
        if (null == token || "".equals(token)){
            return new Result(false,StatusCode.ACCESSERROR,"权限不足");
        }
        problemService.add(problem);
        return new Result(true,StatusCode.OK,"增加成功！");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Problem problem,@PathVariable String id){
        //进行用户鉴权
        String token = (String) request.getAttribute("claims_user");
        if (null == token || "".equals(token)){
            return new Result(false,StatusCode.ACCESSERROR,"权限不足");
        }
        problem.setId(id);
        problemService.update(problem);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        //进行用户鉴权
        String token = (String) request.getAttribute("claims_user");
        if (null == token || "".equals(token)){
            return new Result(false,StatusCode.ACCESSERROR,"权限不足");
        }
        problemService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
